package com.ywq.app.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ywq.app.dao.AdminDao;
import com.ywq.app.domain.AdminResource;
import com.ywq.app.domain.AdminRole;
import com.ywq.app.domain.AdminUser;
import com.ywq.common.exception.AppBusinessException;


public class AdminDaoImpl extends SqlSessionDaoSupport implements AdminDao {
	private final static Logger logger = LoggerFactory
			.getLogger(AdminDaoImpl.class);

	@Override
	public void insertResource(AdminResource adminResource)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AdminMapper.insertResource",
							adminResource);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert resource failure.", e);
			}
			throw new AppBusinessException("Insert resource failure.", e);
		}
	}

	@Override
	public void updateResource(AdminResource adminResource)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AdminMapper.updateResource",
							adminResource);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateResource database failure.", e);
			}
			throw new AppBusinessException("updateResource database failure.",
					e);
		}
	}

	@Override
	public void deleteResourceById(String resourceId)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteResourceById",
							resourceId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete resource failure.", e);
			}
			throw new AppBusinessException("Delete resource failure.", e);
		}
	}

	// 角色
	@Override
	public void insertRole(AdminRole adminRole) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AdminMapper.insertRole",
							adminRole);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert role failure.", e);
			}
			throw new AppBusinessException("Insert role failure.", e);
		}
	}

	@Override
	public void updateRole(AdminRole adminRole) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AdminMapper.updateRole",
							adminRole);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateRole database failure.", e);
			}
			throw new AppBusinessException("updateRole database failure.", e);
		}
	}

	@Override
	public void deleteRoleById(String roleId) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteRoleById",
							roleId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete role failure.", e);
			}
			throw new AppBusinessException("Delete role failure.", e);
		}
	}

	// 用户
	@Override
	public void insertUser(AdminUser adminUser) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AdminMapper.insertUser",
							adminUser);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert user failure.", e);
			}
			throw new AppBusinessException("Insert user failure.", e);
		}
	}

	@Override
	public void updateUser(AdminUser adminUser) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AdminMapper.updateUser",
							adminUser);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateUser database failure.", e);
			}
			throw new AppBusinessException("updateUser database failure.", e);
		}
	}

	@Override
	public void deleteUserById(String userId) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteUserById",
							userId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete user failure.", e);
			}
			throw new AppBusinessException("Delete user failure.", e);
		}
	}

	// 权限集合
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminRole> getAuthorityList() throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getAuthorityList");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAuthorityList database failure.", e);
			}
			throw new AppBusinessException(
					"getAuthorityList database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdminUser> getUserList(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getUserList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get user list failure.", e);
			}
			throw new AppBusinessException("Get user list failure.", e);
		}
	}

	@Override
	public int getAdminUserCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminUserCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Count user failure.", e);
			}
			throw new AppBusinessException("Count user failure.", e);
		}
	}

	// 权限名，资源集合
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminResource> getResourceListByAuthorityName(
			String authorityName) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getResourceListByAuthorityName",
							authorityName);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(
						"getResourceListByAuthorityName database failure.", e);
			}
			throw new AppBusinessException(
					"getResourceListByAuthorityName database failure.", e);
		}

	}

	// 通过用户名得到用户的权限
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminRole> getUserAuthorityList(String username)
			throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getUserAuthorityList",
							username);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(
						"getResourceListByAuthorityName database failure.", e);
			}
			throw new AppBusinessException(
					"getResourceListByAuthorityName database failure.", e);
		}
	}

	// 通过用户名得到用户信息
	@Override
	public AdminUser getUserByUsername(String username)
			throws AppBusinessException {
		try {
			return (AdminUser) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getUserByUsername",
							username);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(
						"getResourceListByAuthorityName database failure.", e);
			}
			throw new AppBusinessException(
					"getResourceListByAuthorityName database failure.", e);
		}

	}

	// 通过用户Id查询Role
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminRole> getAdminUserRole(String id)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getAdminUserRoleList",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAdminUserRole database failure.", e);
			}
			throw new AppBusinessException(
					"getAdminUserRole database failure.", e);
		}
	}

	@Override
	public void insertUserRoleId(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AdminMapper.insertUserRoleId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertUser database failure.", e);
			}
			throw new AppBusinessException("insertUser database failure.", e);
		}
	}

	// 查询所有的role
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminRole> getAdminRole() throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getAdminRoleList");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAdminRole database failure.", e);
			}
			throw new AppBusinessException("getAdminRole database failure.", e);
		}
	}

	@Override
	public int getAdminRoleCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminRoleCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Count role failure.", e);
			}
			throw new AppBusinessException("Count role failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdminRole> getAdminRoleList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getRoleList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get role failure.", e);
			}
			throw new AppBusinessException("Get role failure.", e);
		}
	}

	// 通过roleId查询资源
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminResource> getAdminRoleResource(String id)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getAdminRoleResource",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAdminRoleResource database failure.", e);
			}
			throw new AppBusinessException(
					"getAdminRoleResource database failure.", e);
		}
	}

	// 查询所有的Resource
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminResource> getAdminResource() throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getAdminResourceList");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAdminRole database failure.", e);
			}
			throw new AppBusinessException("getAdminRole database failure.", e);
		}
	}

	// 插入最近的user的role
	@Override
	public void insertUserResourceId(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AdminMapper.insertUserResourceId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertUserResourceId database failure.", e);
			}
			throw new AppBusinessException(
					"insertUserResourceId database failure.", e);
		}
	}

	@Override
	public int getAdminResourceCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminResourceCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Count resource failure.", e);
			}
			throw new AppBusinessException("Count resource failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdminResource> getAdminResourceList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AdminMapper.getResourceList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get resource list failure.", e);
			}
			throw new AppBusinessException("Get resource list failure.", e);
		}
	}

	@Override
	public void deleteUserRoleByUserId(String userId)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteUserRoleByUserId",
							userId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete user-role failure.", e);
			}
			throw new AppBusinessException("Delete user-role failure.", e);
		}
	}

	@Override
	public void deleteUserRoleByRoleId(String roleId)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteUserRoleByRoleId",
							roleId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete user-role failure.", e);
			}
			throw new AppBusinessException("Delete user-role failure.", e);
		}
	}

	@Override
	public void deleteRoleResourceByRoleId(String roleId)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteRoleResourceByRoleId",
							roleId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete role-resource failure.", e);
			}
			throw new AppBusinessException("Delete role-resource failure.", e);
		}
	}

	@Override
	public void deleteRoleResourceByResourceId(String resourceId)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("com.ywq.app.persistence.AdminMapper.deleteRoleResourceByResourceId",
							resourceId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete role-resource failure.", e);
			}
			throw new AppBusinessException("Delete role-resource failure.", e);
		}
	}

	@Override
	public AdminUser getUserById(String id) throws AppBusinessException {
		try {
			return (AdminUser) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminUserById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get user failure.", e);
			}
			throw new AppBusinessException("Get user failure.", e);
		}
	}

	@Override
	public AdminRole getRoleById(String id) throws AppBusinessException {
		try {
			return (AdminRole) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminRoleById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get role failure.", e);
			}
			throw new AppBusinessException("Get role failure.", e);
		}
	}

	@Override
	public AdminResource getResourceById(String id) throws AppBusinessException {
		try {
			return (AdminResource) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AdminMapper.getAdminResourceById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get resource failure.", e);
			}
			throw new AppBusinessException("Get resource failure.", e);
		}
	}
	//查询用户的资源
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminResource> getResourceByUser(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("com.ywq.app.persistence.AdminMapper.getResourceByUser", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getResourceByUser resource failure.", e);
			}
			throw new AppBusinessException("getResourceByUser resource failure.", e);
		}
	}
}
